package com.edu.glutdataconvertserver.listen;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.edu.glutdataconvertcommon.domain.TGlutDcJiankao;
import com.edu.glutdataconvertcommon.domain.TGlutDcXsjbsjzl;
import com.edu.glutdataconvertserver.mapper.auto.TGlutDcJiankaoMapper;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @author ares
 */
@Component
public class ViewKsJiankaoReceiver {

    private TGlutDcJiankaoMapper tGlutDcJiankaoMapper;

    @Autowired
    public ViewKsJiankaoReceiver(TGlutDcJiankaoMapper tGlutDcJiankaoMapper) {
        this.tGlutDcJiankaoMapper = tGlutDcJiankaoMapper;
    }

    @RabbitListener(queues = "ksJiankaoQUEUE")
    public void process(@Payload byte[] message, Message message1, Channel channel) throws IOException {
        try {
            String result = new String(message, "utf-8");
            List<TGlutDcJiankao> tGlutDcJiankaos = JSONArray.parseArray(result, TGlutDcJiankao.class);
            System.out.println("DirectReceiver消费者收到消息  : " + JSON.toJSONString(tGlutDcJiankaos));
            tGlutDcJiankaoMapper.insertList(tGlutDcJiankaos);
            channel.basicAck(message1.getMessageProperties().getDeliveryTag(), true);
        } catch (Exception e) {
            e.printStackTrace();
            channel.basicReject(message1.getMessageProperties().getDeliveryTag(), false);
        }
    }
}